/**

Copyright Jean-Yves MENGANT 1999

this program is Free Software , you can distribute it or modify it
under the term of the GNU General Public Licence as published
by the Free Software Foundation providing you allways provide
this copyright header.

This program is distributed in the hope that it will be usefull but
WITHOUT ANY WARRANTY; without event the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public Licence for
more details.


  This class is responsible of loading basic
  EBCDIC to Ascii Conversion tables based on
  CODE PAGE defined when constructor is called.

  NOTE : for the moment only the "France 00297" is loadable , implementing
  your own country table is straightforward :
  1) locate the private load.... method corresponding to your country inside
  code.
  2) delete the not_implemented_yet(....).
     do a copy/paste of _tAscii2Ebcdic [] = {......} and
                        _tEbcdic2Ascii [] = {......}
     and replace the caracter conversions implemented by the one
     defined in the assoc EBCDIC PAGE code( look at IBM's PAGE CODE doc to proceed )
     THIS IS VERY BORYING BUT YOU SHOULD DO IT.

  3) to load your Ebcdic country table use following code sequence :
     EbcdicTable myTable = new EbcdicTable() ;
     myTable.setPageCode( USA_CANADA_00037 ) ;
     replace of course USA_CANADA_00037 by the one you implemented

  4) I WILL BE PLEASED TO INTEGRATE YOUR TABLE into the code so , please
     send me back your implementation. 

@author Jean-Yves MENGANT

*/

package cobolConversion ;

public class EbcdicTable
implements java.io.Serializable {

   /** Define each known EBCDIC PAGE code here */
   private final static int _CUR_DEFINED_TABLES_    = 9 ; // Increase this guy when adding oses

   public final static String ASCII_USA_00437       = "ASCII USA 00437" ;

   public final static String USA_CANADA_00037      = "USA/Canada 00037" ;
   public final static String INTERNATIONAL1_00256  = "International#1 00256" ;
   public final static String CANADIAN_FRENCH_00260 = "Canadian French 00260" ;
   public final static String GERMANY_AUSTRIA_00273 = "Germany/austria 00273" ;
   public final static String ITALY_00280           = "Italy 00280" ;
   public final static String SPAIN_LATIN_AMCA_00284= "spain/latin america 00284" ;
   public final static String UK_00285              = "United Kingdom 00285" ;
   public final static String FRANCE_00297          = "France 00297" ;
   public final static String MULTINATIONAL_00500   = "Multinational 00500" ;


   private String _currentlyLoaded = FRANCE_00297 ;
   // Define standard Ascii to Ebcdic tables
   private int  _tAscii2Ebcdic [] = {
   0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F, // 07
   0x16,0x05,0x25,0x0B,0x0C,0x0D,0x0E,0x0F, // 0F
   0x10,0x11,0x12,0x13,0xB6,0x5A,0x16,0x17, // 17
   0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, // 1F
   0x40,0x4F,0x7F,0xB1,0x5B,0x6C,0x50,0x7D, // 27
   0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61, // 2F
   0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7, // 37
   0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F, // 3F
   0x44,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7, // 47
   0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6, // 4F
   0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6, // 57
   0xE7,0xE8,0xE9,0x90,0x48,0xB5,0x5F,0x6D, // 5F
   0xA0,0x81,0x82,0x83,0x84,0x85,0x86,0x87, // 67
   0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96, // 6F
   0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6, // 77
   0xA7,0xA8,0xA9,0x51,0xBB,0x54,0xBD,0x7F, // 7F
   0x68,0xDC,0xC0,0x42,0x43,0x7C,0x47,0xE0, // 87
   0x52,0x53,0xD0,0x57,0x56,0x55,0x63,0x67, // 8F
   0x71,0x9C,0x9E,0xCB,0xCC,0xCD,0xDB,0x6A, // 97
   0xDF,0xEC,0xFC,0xB0,0x7B,0xB2,0xFF,0xFF, // 9F
   0x45,0x55,0xCE,0xDE,0x49,0x69,0x9A,0x9B, // A7
   0xAB,0xA9,0xBA,0xB8,0xB7,0xAA,0x8A,0x8B, // AF
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // B7
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // BF
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // C7
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // CF
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // D7
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // DF
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // E7
   0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // EF
   0xFF,0x8F,0xFF,0xFF,0xFF,0xFF,0xE1,0xFF, // F7
   0xFF,0xFF,0xB3,0xFF,0xFF,0xFF,0xFF,0xFF  // FF
   };

   // Define standard Ebcdic to Ascii conversion tables
   private int _tEbcdic2Ascii [] = {
   0x00,0x01,0x02,0x03,0xFF,0x09,0xFF,0x7F, // 07
   0xFF,0xFF,0xFF,0x0B,0x0C,0x0D,0x0E,0x0F, // 0F
   0x10,0x11,0x12,0x13,0xFF,0xFF,0x08,0xFF, // 17
   0x18,0x19,0xFF,0xFF,0xFF,0x1D,0xFF,0x1F, // 1F
   0xFF,0xFF,0x1C,0xFF,0xFF,0x0A,0x17,0x1B, // 27
   0xFF,0xFF,0xFF,0xFF,0xFF,0x05,0x06,0x07, // 2F
   0xFF,0xFF,0x16,0xFF,0xFF,0x1E,0xFF,0x04, // 37
   0xFF,0xFF,0xFF,0xFF,0x14,0x15,0xFF,0x1A, // 3F
   0x20,0xFF,0x83,0x84,0x40,0xA0,0xFF,0x86, // 47
   0x5C,0xA4,0xF8,0x2E,0x3C,0x28,0x2B,0x21, // 4F
   0x26,0x7B,0x88,0x89,0x7D,0xA1,0x8C,0x8B, // 57
   0x8D,0xE1,0x15,0x24,0x2A,0x29,0x3B,0x5E, // 5F
   0x2D,0x2F,0xFF,0x8E,0xFF,0xFF,0xFF,0x8F, // 67
   0x80,0xA5,0x97,0x2C,0x25,0x5F,0x3E,0x3F, // 6F
   0xFF,0x90,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // 77
   0xFF,0xE6,0x3A,0x9C,0x85,0x27,0x3D,0x22, // 7F
   0xFF,0x61,0x62,0x63,0x64,0x65,0x66,0x67, // 87
   0x68,0x69,0xAE,0xAF,0xFF,0xFF,0xFF,0xFF, // 8F
   0x5B,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70, // 97
   0x71,0x72,0xA6,0xA7,0x91,0xFF,0x92,0xFF, // 9F
   0xFF,0xFF,0x73,0x74,0x75,0x76,0x77,0x78, // A7
   0x79,0x7A,0xAD,0xA8,0xFF,0xFF,0xFF,0xFF, // AF
   0x9B,0x23,0x9D,0xF9,0xFF,0x5D,0x14,0xAC, // B7
   0xAB,0xFF,0xAA,0x7C,0xFF,0x7E,0xFF,0xFF, // BF
   0x82,0x41,0x42,0x43,0x44,0x45,0x46,0x47, // C7
   0x48,0x49,0xFF,0x93,0x94,0x95,0xA2,0xFF, // CF
   0x8A,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,0x50, // D7
   0x51,0x52,0xFF,0x96,0x81,0xFF,0xA3,0x98, // DF
   0x87,0xF6,0x53,0x54,0x55,0x56,0x57,0x58, // E7
   0x59,0x5A,0xFF,0x99,0xFF,0xFF,0xFF,0xFF, // EF
   0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37, // F7
   0x38,0x39,0xFF,0x9A,0xFF,0xFF,0xFF,0xFF  // FF
   };

  // Just implement differences
  private void not_implemented_yet ( String what )
  {
    System.out.println(what+" not yet implemented");
    System.out.println("system defaukted to FRANCE_00297");
  }

  private void loadInternational_00256()
  {
    not_implemented_yet("Ebcdic International 00256");
  }

  private void loadCanadianFrench_260()
  {
    not_implemented_yet("Canadian Franch 00260");
  }

  private void loadGermanyAustria_00273()
  {
    not_implemented_yet("German Austria 00273");
  }

  private void loadItaly_00280()
  {
    not_implemented_yet("italy 00280");
  }

  private void loadSpainLatinAmca_00284()
  {
    not_implemented_yet("Latin Americ 00284");
  }

  private void loadUk_00285()
  {
    not_implemented_yet("United Kingdom 00285");
  }

  private void loadUsaCanada_00037()
  {
    not_implemented_yet("USA Canada 00037");
  }

  private void loadMultinational_00500()
  {
    not_implemented_yet("Multinational 500");
  }

  /** Default constructor implements FRANCE_0297     */
  public EbcdicTable()
  {}

  public void set_pageCode( String pageCode )
  {
    if ( pageCode.equalsIgnoreCase(ASCII_USA_00437) )
      return ; // Just return with nothing set when ASCII
    if ( pageCode.equalsIgnoreCase( USA_CANADA_00037) )
      loadUsaCanada_00037() ;
    else if ( pageCode.equalsIgnoreCase( INTERNATIONAL1_00256 ) )
      loadInternational_00256() ;
    else if ( pageCode.equalsIgnoreCase( CANADIAN_FRENCH_00260 ) )
      loadCanadianFrench_260() ;
    else if ( pageCode.equalsIgnoreCase( GERMANY_AUSTRIA_00273 ) )
      loadGermanyAustria_00273() ;
    else if ( pageCode.equalsIgnoreCase( ITALY_00280 ) )
      loadItaly_00280() ;
    else if ( pageCode.equalsIgnoreCase( SPAIN_LATIN_AMCA_00284 ) )
      loadSpainLatinAmca_00284() ;
    else if ( pageCode.equalsIgnoreCase( UK_00285 ) )
      loadUk_00285() ;
    else if ( pageCode.equalsIgnoreCase( FRANCE_00297 ) )
      ; // private tables defaulted to FRANCE_00297 already here
    else if ( pageCode.equalsIgnoreCase( MULTINATIONAL_00500 ) )
      loadMultinational_00500() ;
    else
      throw new RuntimeException("JVCICS1 : Undefined Ebcdic table : "
                                 + pageCode ) ;
    _currentlyLoaded = pageCode ;
  }

  public String get_pageCode()
  { return _currentlyLoaded ; }

  /** Single accessors */
  public int[] get_tAscii2Ebcdic()
  { return _tAscii2Ebcdic ; }
  public int[] get_tEbcdic2Ascii()
  { return _tEbcdic2Ascii ; }

  /**
    The toString method just send back the name
    of the currently loaded pageCode
  */
  public String toString()
  { return _currentlyLoaded ; }

  /**
    This public static method just returns a vector of availables
    defined TABLES inside the system
  */
  public static String[] giveAvailableEbcdcicTables()
  {
  String returned[] = new String[_CUR_DEFINED_TABLES_] ;
    returned[0] = USA_CANADA_00037 ;
    returned[1] = INTERNATIONAL1_00256 ;
    returned[2] = CANADIAN_FRENCH_00260 ;
    returned[3] = GERMANY_AUSTRIA_00273 ;
    returned[4] = ITALY_00280  ;
    returned[5] = SPAIN_LATIN_AMCA_00284 ;
    returned[6] = UK_00285  ;
    returned[7] = FRANCE_00297   ;
    returned[8] = MULTINATIONAL_00500 ;
    return returned ;
  }
}

